<template>
  <div class="dialog-form">
    <!-- 添加或修改弹出框 -->
    <el-dialog
      :title="title"
      center
      :visible.sync="open"
      @close="close(false)"
      width="600px"
      append-to-body
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item
          :label="$t('batch.missionManage.form.processCode')"
          prop="flowCode"
        >
          <el-input
            v-model="editForm.flowCode"
            :placeholder="$t('batch.missionManage.rules.pleaseEnter')"
            :disabled="disabled"
          />
        </el-form-item>
        <el-form-item
          :label="$t('batch.missionManage.form.processName')"
          prop="flowName"
        >
          <el-input
            v-model="editForm.flowName"
            :placeholder="$t('batch.missionManage.rules.pleaseEnter')"
            :disabled="disabled"
          />
        </el-form-item>
        <el-form-item
          :label="$t('batch.workflowConfig.form.flagType')"
          prop="flagType"
        >
          <el-select
            :disabled="disabled"
            v-model="editForm.flagType"
            :placeholder="$t('batch.missionManage.rules.pleaseSelect')"
            style="width: 100%"
          >
            <el-option
              v-for="item in dictMap.scubb_flag_type"
              :key="item.value"
              :label="item.text"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item
          :label="$t('batch.workflowConfig.form.whetherShare')"
          prop="unShared"
        >
          <el-select
            :disabled="disabled"
            v-model="editForm.unShared"
            :placeholder="$t('batch.missionManage.rules.pleaseSelect')"
            style="width: 100%"
          >
            <el-option
              v-for="item in dictMap.scubb_unshared"
              :key="item.value"
              :label="item.text"
              :value="Number(item.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          :label="$t('batch.workflowConfig.form.isItEffective')"
          prop="isDeleted"
        >
          <el-select
            :disabled="disabled"
            v-model="editForm.isDeleted"
            :placeholder="$t('batch.missionManage.rules.pleaseSelect')"
            style="width: 100%"
          >
            <el-option
              v-for="item in dictMap.scubb_is_deleted"
              :key="item.value"
              :label="item.text"
              :value="Number(item.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('common.remark')" prop="remark">
          <el-input
            :disabled="disabled"
            v-model="editForm.remark"
            type="textarea"
            :placeholder="$t('batch.missionManage.rules.pleaseEnter')"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!disabled">{{
          $t("common.confirm")
        }}</el-button>
        <el-button @click="close">{{ $t("common.cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  get,
  save,
  update,
  checkFlowNameUnique,
  checkFlowCodeUnique,
} from "@/api/batch_app/workflowConfig";
import { dialogMixin } from "@/libs/dialogMixin";
export default {
  name: "EditWorkflow",
  mixins: [dialogMixin],
  data() {
    // 表单校验
    let checkFlowCodeUniqueRule = (rule, value, callback) => {
      if (!value) {
        callback(new Error(this.$t("batch.systemConfig.rules.cannotEmpty")));
      } else {
        checkFlowCodeUnique({
          flowCode: this.editForm.flowCode,
          id: this.editForm.id,
        }).then((response) => {
          var result = response.result;
          if (!result) {
            callback(
              new Error(this.$t("batch.systemConfig.rules.alreadyExists"))
            );
          } else {
            callback();
          }
        });
      }
    };

    let checkFlowNameUniqueRule = (rule, value, callback) => {
      if (!value) {
        callback(new Error(this.$t("batch.systemConfig.rules.cannotEmpty")));
      } else {
        checkFlowNameUnique({
          flowName: this.editForm.flowName,
          id: this.editForm.id,
        }).then((response) => {
          var result = response.result;
          if (!result) {
            callback(
              new Error(this.$t("batch.systemConfig.rules.alreadyExists"))
            );
          } else {
            callback();
          }
        });
      }
    };
    return {
      open: false,
      disabled: false,
      rules: {
        flowCode: [
          {
            required: true,
            trigger: "blur",
            validator: checkFlowCodeUniqueRule,
          },
        ],
        flowName: [
          {
            required: true,
            trigger: "blur",
            validator: checkFlowNameUniqueRule,
          },
        ],
        flagType: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("batch.systemConfig.rules.mustChoose"),
          },
        ],
        unShared: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("batch.systemConfig.rules.mustChoose"),
          },
        ],
        isDeleted: [
          {
            required: true,
            trigger: "blur",
            message: this.$t("batch.systemConfig.rules.mustChoose"),
          },
        ],
        remark: [
          {
            max: 500,
            message: this.$t("batch.systemConfig.rules.maxLength500"),
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.loadDicts(["scubb_flag_type", "scubb_unshared", "scubb_is_deleted"]);
  },
  methods: {
    show(id, disabled = false) {
      this.disabled = disabled;
      this.$set(this, "editForm", {});
      if (id) {
        this.title = this.$t("common.edit");
        get(id)
          .then((res) => {
            this.$set(this, "editForm", res.result);
            delete this.editForm.flowNotifyClass;
            delete this.editForm.configJsonPath;
            this.open = true;
          })
          .catch((err) => {
            this.$message.error(
              this.$t("batch.workflowConfig.form.dataException")
            );
          });
      } else {
        this.title = this.$t("common.add");
        this.open = true;
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          if (this.editForm.id) {
            delete this.editForm.configJsonPath;
            delete this.editForm.createTime;
            delete this.editForm.updateTime;
            update(this.editForm).then((res) => {
              this.msgSuccess(this.$t("common.modifySuccess"));
              this.close(true);
            });
          } else {
            delete this.editForm.configJsonPath;
            delete this.editForm.createTime;
            delete this.editForm.updateTime;
            save(this.editForm).then((res) => {
              this.msgSuccess(this.$t("common.addSuccess"));
              this.close(true);
            });
          }
        }
      });
    },
    // 取消按钮
    close(state = false) {
      this.open = false;
      this.resetForm("editForm");
      if (state) this.$emit("editComplete", state);
    },
  },
};
</script>

<style scoped>
</style>

